package com.winit.page.web;

import com.winit.common.base.bean.ExecuteResult;
import com.winit.common.base.web.BaseController;
import com.winit.page.manager.PageManager;
import com.winit.page.model.Page;
import com.winit.page.model.PageElement;
import io.swagger.annotations.Api;
import io.swagger.annotations.ApiOperation;
import io.swagger.annotations.ApiParam;
import org.slf4j.Logger;
import org.slf4j.LoggerFactory;
import org.springframework.beans.factory.annotation.Autowired;
import org.springframework.cache.annotation.CacheEvict;
import org.springframework.cache.annotation.Cacheable;
import org.springframework.cache.annotation.Caching;
import org.springframework.http.HttpStatus;
import org.springframework.util.StringUtils;
import org.springframework.web.bind.annotation.*;

import java.util.Map;

@RestController
@Api(value = "page", tags = {"page"}, description = "page")
@RequestMapping(value = "/api/v1/page")
public class PageController extends BaseController {

    private static Logger logger = LoggerFactory.getLogger(PageController.class);

    @Autowired
    private PageManager pageManager;

    /**
     * @param menuId
     * @return
     */
    @RequestMapping(value = "/getMenuPages", method = RequestMethod.GET)
    @ResponseStatus(HttpStatus.OK)
    @ApiOperation(value = "获取菜单页面")
    @ResponseBody
    public Map getMenuPages(@ApiParam(value = "menuId", required = false) @RequestParam(value = "menuId", required = false) String menuId,
                            @ApiParam(value = "pageId", required = false) @RequestParam(value = "pageId", required = false) String pageId) {
        if (StringUtils.isEmpty(menuId) && StringUtils.isEmpty(pageId)) {
            return this.getRtn(REQUEST_ERROR, "参数不能同时为空");
        }

        ExecuteResult executeResult = this.pageManager.getMenuPages(menuId,pageId, true);
        return this.getRtn(executeResult.getCode(), executeResult.getMsg(), executeResult.getData());
    }
    /**
     * @param
     * @return
     */
    @RequestMapping(value = "/getAllPages", method = RequestMethod.GET)
    @ResponseStatus(HttpStatus.OK)
    @ApiOperation(value = "获取所有页面列表")
    @ResponseBody
    public Map getAllPages() {
        ExecuteResult executeResult = this.pageManager.getAllPages();
        return this.getRtn(executeResult.getCode(), executeResult.getMsg(), executeResult.getData());
    }


    @RequestMapping(value = "/savePage", method = RequestMethod.POST)
    @ResponseStatus(HttpStatus.OK)
    @ApiOperation(value = "新建菜单页面")
    @CacheEvict(cacheNames = {"feature_base_tree","getAllElements"}, allEntries = true)
    @ResponseBody
    public Map savePage(@RequestBody Page page) {
        ExecuteResult executeResult = this.pageManager.savePage(page);
        return this.getRtn(executeResult.getCode(), executeResult.getMsg(), executeResult.getData());
    }


    @RequestMapping(value = "/getPageElements", method = RequestMethod.GET)
    @ResponseStatus(HttpStatus.OK)
    @ApiOperation(value = "获取菜单页面元素")
    @ResponseBody
    @Cacheable(cacheNames = {"tree_menu_page_element"}, key = "#pageId")
    public Map getPageElements(@ApiParam(value = "pageId", required = true) @RequestParam(value = "pageId", required = true) String pageId) {
        ExecuteResult executeResult = this.pageManager.getPageElements(pageId);
        return this.getRtn(executeResult.getCode(), executeResult.getMsg(), executeResult.getData());
    }

    @RequestMapping(value = "/getPageElementsInfo", method = RequestMethod.GET)
    @ResponseStatus(HttpStatus.OK)
    @ApiOperation(value = "获取菜单页面详情")
    @ResponseBody
    public Map getPageElementsInfo(@ApiParam(value = "elementId", required = false) @RequestParam(value = "elementId", required = true) String elementId) {
        ExecuteResult executeResult = this.pageManager.getPageElementsInfo(elementId);
        return this.getRtn(executeResult.getCode(), executeResult.getMsg(), executeResult.getData());
    }


    @RequestMapping(value = "/createPageElement", method = RequestMethod.POST)
    @ResponseStatus(HttpStatus.OK)
    @ApiOperation(value = "新增pageElement")
    @ResponseBody
    @CacheEvict(cacheNames = {"tree_menu_page_element","getAllElements"}, allEntries = true)
    public Map createPageElement(@RequestBody PageElement pageElement) {
        ExecuteResult executeResult = this.pageManager.createPageElement(pageElement);
        return this.getRtn(executeResult.getCode(), executeResult.getMsg(), executeResult.getData());
    }

    @RequestMapping(value = "/updatePageElementInfo", method = RequestMethod.POST)
    @ResponseStatus(HttpStatus.OK)
    @ApiOperation(value = "更新页面元素")
    @ResponseBody
    @CacheEvict(cacheNames = {"tree_menu_page_element","getAllElements"}, allEntries = true)
    public Map updatePageElementInfo(@ApiParam(value = "elementId", required = false) @RequestParam(value = "elementId", required = true) String elementId,
                                     @ApiParam(value = "elementAttrs", required = false) @RequestParam(value = "elementAttrs", required = true) String elementAttrs) {
        ExecuteResult executeResult = this.pageManager.updatePageElementInfo(elementId, elementAttrs);
        return this.getRtn(executeResult.getCode(), executeResult.getMsg(), executeResult.getData());
    }

    @RequestMapping(value = "/deletePageElement", method = RequestMethod.GET)
    @ResponseStatus(HttpStatus.OK)
    @ApiOperation(value = "删除页面元素")
    @ResponseBody
    @CacheEvict(cacheNames = {"tree_menu_page_element","getAllElements"}, allEntries = true)
    public Map deletePageElement(@ApiParam(value = "elementId", required = false) @RequestParam(value = "elementId", required = true) String elementId) {
        ExecuteResult executeResult = this.pageManager.deletePageElement(elementId);
        return this.getRtn(executeResult.getCode(), executeResult.getMsg(), executeResult.getData());
    }

    @RequestMapping(value = "/deletePage", method = RequestMethod.GET)
    @ResponseStatus(HttpStatus.OK)
    @ApiOperation(value = "删除页面")
    @CacheEvict(cacheNames = {"feature_base_tree","getAllElements"}, allEntries = true)
    @ResponseBody
    public Map deletePage(@ApiParam(value = "pageId", required = false) @RequestParam(value = "pageId", required = true) String pageId,
                          @ApiParam(value = "deleteAllFeature, 0-否，1-是.（缺省为0）", required = false) @RequestParam(value = "deleteAllFeature", defaultValue = "0") String deleteAllFeature) {
        boolean deleteAllPageFeature = "1".equalsIgnoreCase(deleteAllFeature) ? true : false;
        ExecuteResult executeResult = this.pageManager.deletePage(pageId, deleteAllPageFeature);
        return this.getRtn(executeResult.getCode(), executeResult.getMsg(), executeResult.getData());
    }

    @RequestMapping(value = "/applyPageConfig", method = RequestMethod.GET)
    @ResponseStatus(HttpStatus.OK)
    @ApiOperation(value = "调用wsAgent刷新页面")
    @ResponseBody
    public Map applyPageConfig(@ApiParam(value = "pageId", required = false) @RequestParam(value = "pageId", required = true) String pageId) {
        ExecuteResult executeResult = this.pageManager.applyPageConfig(pageId);
        return this.getRtn(executeResult.getCode(), executeResult.getMsg(), executeResult.getData());
    }


    @RequestMapping(value = "/evictElementTreeCache", method = RequestMethod.GET)
    @ResponseStatus(HttpStatus.OK)
    @ApiOperation(value = "刷新page element tree的缓存")
    @ResponseBody
    @Caching(evict = {
            @CacheEvict(cacheNames = {"tree_menu_page_element"}, allEntries = true, condition = "#pageId == null"),
            @CacheEvict(cacheNames = {"tree_menu_page_element"}, key = "#pageId + ''")
    })
    public Map evictPageElementTreeCache(@ApiParam(value = "pageId, null时清除所有element tree cache", required = false) @RequestParam(value = "pageId", required = false) String pageId) {
        ExecuteResult executeResult = new ExecuteResult();
        return this.getRtn(executeResult.getCode(), executeResult.getMsg(), executeResult.getData());
    }

    @RequestMapping(value = "/childSort", method = RequestMethod.POST)
    @ResponseStatus(HttpStatus.OK)
    @ApiOperation(value = "保存子节点顺序")
    @ResponseBody
    public Map saveChildSort(@ApiParam(value = "id", required = true) @RequestParam(value = "id", required = true) String id,
                             @ApiParam(value = "sort", required = true) @RequestParam(value = "sort", required = true) String sort) {
        pageManager.saveChildSort(id,sort);
        return this.getRtn(REQUEST_SUCCESS, "");
    }

}
